package org.wisteria.framework.web.mvc.service;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.wisteria.framework.web.QueryResult;
import org.wisteria.framework.web.mvc.dao.SuperMapper;
import org.wisteria.framework.web.mvc.vo.ConditionVO;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Date;

import static org.wisteria.framework.web.mvc.constant.ColumnConsts.*;

/**
 * @author Lei.Liu
 * @param <M>   mapper 对象
 * @param <T>   实体
 */
public abstract class AbstractSuperService<M extends SuperMapper<T>, T> extends ServiceImpl<M, T> implements ISuperService<T> {

    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractSuperService.class);

    /**
     * 分页查询
     *
     * @param condition 查询条件
     * @param pageNum   页码
     * @param pageSize  每页条数
     * @return
     */
    @Override
    public QueryResult pageList(ConditionVO condition, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page page = this.baseMapper.selectListPage(condition);
        return new QueryResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除数据并记录操作信息
     *
     * @param ids          ID集合
     * @param modifiedId   操作人ID
     * @param modifiedName 操作人名称
     */
    @Override
    public boolean removeByIds(Collection<? extends Serializable> ids, String modifiedId, String modifiedName) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }

        String tableIdColumn = getTableIdColumn();
        if (!StringUtils.hasText(tableIdColumn)) {
            LOGGER.error(this.currentModelClass().getName() + " no primary key.");
            return false;
        }

        UpdateWrapper<T> wrapper = new UpdateWrapper();
        wrapper.set(IS_DELETED_COLUMN, 1);
        wrapper.set(MODIFIED_TIME_COLUMN, new Date());
        wrapper.set(MODIFIED_ID_COLUMN, modifiedId);
        wrapper.set(MODIFIED_NAME_COLUMN, modifiedName);
        wrapper.in(tableIdColumn, ids);
        try {
            return this.update(this.currentModelClass().newInstance(), wrapper);
        } catch (Exception e) {
            LOGGER.error(this.currentModelClass().getName() + " remove fail.", e);
        }
        return false;
    }

    /**
     * 获取实体类对应的表的主键字段名称
     * @return  String
     */
    protected String getTableIdColumn() {
        Field[] fields = this.currentModelClass().getDeclaredFields();
        if (fields != null && fields.length > 0) {
            for (Field field : fields) {
                TableId tableId = field.getAnnotation(TableId.class);
                if (tableId != null) {
                    return tableId.value();
                }
            }
        }
        return null;
    }
}
